using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Admin_EditProduct : System.Web.UI.Page
{   

    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void btnSaveImage_Click(object sender, EventArgs e)
    {
        HttpPostedFile myPostedFile = myFile.PostedFile;
        
        if (myPostedFile != null)
        {
            string fileUploadName = myPostedFile.FileName;
            if (!string.IsNullOrEmpty(fileUploadName))
            {
                try
                {
                    int size = int.Parse(txtSize.Text);
                    SaveImage(myPostedFile, fileUploadName, size);
                    previewImage.Visible = true;
                    previewImage.ImageUrl = @"../CoreLayout/ProductImages/" + fileUploadName;
                }
                catch (System.FormatException fe)
                {
                    if (fe.Message != null)
                    {
                        previewImage.Visible = true;
                        SaveImage(myPostedFile, fileUploadName, 300);
                        previewImage.ImageUrl = @"../CoreLayout/ProductImages/" + fileUploadName;
                    }
                }
            }
            else
            {
                lblValidate.Text = "You have to choose a picture file !";
            }
        }
        else
        {
            lblValidate.Text = "No file !";
        }

    }

    private void SaveImage(HttpPostedFile postedFile, string clientFileName, int imageSize)
    {
        string savedFolder = @"CoreLayout/ProductImages/";
        string realSaveFolder = Server.MapPath(savedFolder);
        string serverPath = realSaveFolder.Remove(17, 6);
        string uploadedFileName = serverPath + clientFileName;
        string extention = Path.GetExtension(uploadedFileName);

        if (extention == ".jpg" || extention == ".bmp" || extention == ".gif" || extention == ".png")
        {
            Bll.ProductBll proBll = new Bll.ProductBll();
            proBll.SaveUploadImage(postedFile, uploadedFileName, imageSize);

            string path = @"~/CoreLayout/ProductImages/";
            bool result = proBll.UpdatePicture(path + clientFileName, ddlstProduct.SelectedValue);
            if (result)
                lblValidate.Text = "File has been sent !";
            else
                lblValidate.Text = "Can not send file !";
        }
        else
        {
            lblValidate.Text = "Only jpg/bmp/gif/png file extention is allowed";
        }
    }

    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.AffectedRows > 0)
            lblResult.Text = "Update Successfuly !";
        else
            lblResult.Text = "Update Fail !";
        ddlstProduct.DataBind();
    }
    protected void ddlstProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        ObjectDataSource1.SelectParameters[0].DefaultValue = ddlstProduct.SelectedValue;
    }
}
